package graph.group.zhangtao.guider.getter;

import android.media.MediaPlayer;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 音频文件获取
 * Created by Me on 2017/12/26.
 */

public class MediaGetterFactory {
    private MediaPlayer mediaPlayer;
    private String path;
    private String fromFile;
    public MediaGetterFactory(String path, MediaPlayer mediaPlayer,boolean fromfile) {
        this.path = path;
        this.mediaPlayer = mediaPlayer;
        if(fromfile){
            File file = new File(path);
            try {
                mediaPlayer
                        .setDataSource(file.getPath());
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
                mediaPlayer = null;
            }
        }else {
            try {
                mediaPlayer
                        .setDataSource(path);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
                mediaPlayer = null;
            }
        }
    }

//    public MediaGetterFactory(String path, String id, String extendPath, MediaPlayer mediaPlayer) {
//        try {
//            String tempId = URLEncoder.encode(id, "UTF-8");
//            path = path + tempId + "/" + extendPath;
//            this.path = path;
//            this.mediaPlayer = mediaPlayer;
//            try {
//                mediaPlayer
//                        .setDataSource(path);
//                mediaPlayer.prepare();
//            } catch (IOException e) {
//                e.printStackTrace();
//                mediaPlayer = null;
//            }
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//    }

    public boolean play() {
        if (mediaPlayer != null) {
            mediaPlayer.start();
            return true;
        }
        return false;
    }

    public boolean pause() {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
            return true;
        }
        return false;
    }

}
